package com.zhudoo.zhilian.service;

import com.zhudoo.zhilian.dao.entity.AccessLog;
import com.zhudoo.zhilian.dao.entity.User;
import com.zhudoo.zhilian.dao.impl.AccessDao;
import com.zhudoo.zhilian.dao.impl.UserDao;
import com.zhudoo.zhilian.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author zhudao
 */
@Service
public class AccessLogService {

    /**
     * 用户
     */
    public static int B_T_USER = 1;
    /**
     * 帖子
     */
    public static int B_T_TIP = 2;


    @Autowired
    AccessDao accessDao;
    @Autowired
    UserDao userDao;

    @Async
    public void addAccessLog(String token, int businessType, int businessId, String businessOperating) {
        AccessLog accessLog = new AccessLog();
        accessLog.setCreateTime(new Date());
        accessLog.setBusinessId(businessId);
        accessLog.setBusinessOperating(businessOperating);
        accessLog.setBusinessType(businessType);
        if (StringUtils.isNotEmpty(token)) {
            User user = userDao.findUserByToken(token);
            accessLog.setUserId(user.getId());
            accessLog.setUserType(user.getType());
        }
        accessDao.saveAccessLog(accessLog);
    }
}
